package com.chinawlx.wlxteacher.utils;

import com.chinawlx.wlxteacher.network.bean.WLXCalendarBean;
import com.chinawlx.wlxteacher.network.bean.WLXTimeLineBean;
import com.chinawlx.wlxteacher.wlx_grade_timeline;
import com.chinawlx.wlxteacher.wlx_grade_timelineDao;
import com.chinawlx.wlxteacher.wlx_schedule;
import com.chinawlx.wlxteacher.wlx_scheduleDao;
import com.chinawlx.wlxteacher.wlx_user;
import com.chinawlx.wlxteacher.wlx_userDao;
import com.google.gson.Gson;

import java.util.List;

/**
 * 作者:吴名
 * 时间:16/8/12
 * 描述:存数据到SQLite数据库中指定表
 */
public class WLXSaveSQLiteUtil {
    /**
     * 存储数据到wlx_schedule(课程表里面存储)
     *
     * @param schedule_list
     */
    public static void saveWlxSchedule(List<WLXCalendarBean.DataBean> schedule_list) {
        for (WLXCalendarBean.DataBean schedule : schedule_list) {
            String schedule_id = schedule.getId() + "";
            int teacher_user_id = schedule.getUser_id();
            int org_user_id = schedule.getOrg_user_id();
            int addition_id = schedule.getAddition_id();
            String addition_student_ids = schedule.getAddition_student_ids();
            int space_id = schedule.getSpace_id();
            int classroom_id = schedule.getClassroom_id();
            int grade_id = schedule.getGrade_id();
            String grade_title = schedule.getGrade_title();
            int score_well = schedule.getScore_well();
            int score_good = schedule.getScore_good();
            int score_bad = schedule.getScore_bad();
            String prepare_content = schedule.getPrepare_content();
            String remark = schedule.getRemark();
            String class_type_code = "";
            WLXCalendarBean.DataBean.ClassTypeBean classType = schedule.getClass_type();
            if (classType != null) {
                class_type_code = classType.getCode();
            }
            String class_child_type_code = "";
            WLXCalendarBean.DataBean.ClassChildTypeBean classChildType = schedule
                    .getClass_child_type();
            if (classChildType != null) {
                class_child_type_code = classChildType.getCode();
            }
            String grade_type_code = "";
            WLXCalendarBean.DataBean.GradeTypeBean gradeType = schedule.getGrade_type();
            if (gradeType != null) {
                grade_type_code = gradeType.getKey();
            }
            int is_study = schedule.isIs_study() ? 1 : 0;
            int is_homework = schedule.isIs_homework() ? 1 : 0;
            int is_addition = schedule.isIs_addition() ? 1 : 0;
            int is_finished = schedule.isIs_finished() ? 1 : 0;
            String extend_info = new Gson().toJson(schedule.getExtend());
            long opening_date = WLXDateUtil.getStringToDate(schedule.getOpening_date());
            long closing_date = WLXDateUtil.getStringToDate(schedule.getClosing_date());
            long schedule_date = WLXDateUtil.getStringToDate(schedule.getSchedule_date());
            long last_modified_date = WLXDateUtil.getStringToDate(schedule.getLast_modified_date());

            wlx_schedule sch = new wlx_schedule(schedule_id, teacher_user_id, org_user_id,
                    addition_id, addition_student_ids, space_id, classroom_id, grade_id,
                    grade_title, score_well, score_good, score_bad, prepare_content, remark,
                    class_type_code, class_child_type_code, grade_type_code, is_study,
                    is_homework, is_addition, is_finished, extend_info, opening_date,
                    closing_date, schedule_date, last_modified_date);

            List<wlx_schedule> scheduleList = WLXGreenDaoUtil.getScheduleDao().queryBuilder()
                    .where(wlx_scheduleDao.Properties.Schedule_id.eq(schedule_id)).list();
            if (scheduleList.size() > 0) {
                if (last_modified_date > scheduleList.get(0).getLast_modified_date()) {
                    //更新数据库
                    WLXGreenDaoUtil.getScheduleDao().update(sch);
                }
            } else {
                //插入数据库
                WLXGreenDaoUtil.getScheduleDao().insert(sch);
            }
        }
    }


    /**
     * 存储数据到wlx_grade_timeline
     *
     */
    public static void saveWlxGradeTimeline(List<WLXTimeLineBean.Data.TimelineList> timelineList) {
        for (WLXTimeLineBean.Data.TimelineList timeLine : timelineList) {
            String grade_timeline_id = timeLine.getId() + "";
            int student_id = timeLine.getStudent_id();
            int teacher_user_id = timeLine.getTeacher_user_id();
            int grade_id = timeLine.getGrade_id();
            int resource_id = timeLine.getResource_id();
            String content = timeLine.getContent();
            String type_code = "";
            if (timeLine.getType() != null) {
                type_code = timeLine.getType().getKey();
            }
            String extend_info = "";
            if (timeLine.getExtend() != null) {
                extend_info = new Gson().toJson(timeLine.getExtend());
            }
            long last_modified_date = WLXDateUtil.getStringToDate(timeLine.getLast_modified_date());
            long creation_date = WLXDateUtil.getStringToDate(timeLine.getCreation_date());

            wlx_grade_timeline gradeTimeLine = new wlx_grade_timeline(grade_timeline_id,
                    student_id, teacher_user_id, grade_id, resource_id, content, type_code,
                    extend_info, last_modified_date, creation_date);
            if (timeLine.getExtend().getResource_status_code().equals("deleted")) {
                WLXGreenDaoUtil.getGradeTimeLineDao().delete(gradeTimeLine);
            } else {
                List<wlx_grade_timeline> list = WLXGreenDaoUtil.getGradeTimeLineDao()
                        .queryBuilder().where(wlx_grade_timelineDao.Properties.Grade_timeline_id
                                .eq(grade_timeline_id)).list();
                if (list.size() > 0) {
                    wlx_grade_timeline timeline = list.get(0);
                    if (last_modified_date > timeline.getLast_modified_date()) {
                        WLXGreenDaoUtil.getGradeTimeLineDao().update(gradeTimeLine);
                    }
                } else {
                    //作业是显示(未删除状态才加入到数据库)
                    WLXGreenDaoUtil.getGradeTimeLineDao().insert(gradeTimeLine);
                }
            }
        }

    }

    public static void saveWlxUser(WLXTimeLineBean.Data.FamilyUser family_user) {
        String user_id = family_user.getProfile().getUser_id()+"";
        String mobile = family_user.getMobile();
        String nickname = family_user.getProfile().getNickname();
        String realname = family_user.getProfile().getRealname().getName();
        String birthday = family_user.getProfile().getBirthday();
        String avatar_url = family_user.getProfile().getAvatar().getUrl();
        String qq = family_user.getProfile().getQq();
        int level = family_user.getProfile().getLevel().getLevel();
        int score = family_user.getProfile().getLevel().getScore();
        String sex_code = family_user.getProfile().getSex().getValue();
        String user_type_code = family_user.getProfile().getUser_type()
                .getValue();
        long last_modified_date = WLXDateUtil.getStringToDate(family_user
                .getProfile().getLast_modified_date());

        wlx_user wlxUser = new wlx_user(user_id, mobile, nickname, realname,
                WLXDateUtil.getStringToDate(birthday), avatar_url, qq, level, score, sex_code,
                user_type_code, last_modified_date);
        List<wlx_user> list = WLXGreenDaoUtil.getUserDao().queryBuilder().where(wlx_userDao
                .Properties.User_id.eq(user_id)).list();
        if (list.size() > 0) {
            if (last_modified_date > list.get(0).getLast_modified_date()) {
                WLXGreenDaoUtil.getUserDao().update(wlxUser);
            }
        } else {
            WLXGreenDaoUtil.getUserDao().insert(wlxUser);
        }
    }
}
